package com.codeshellme.bookmanager.controller;


import com.codeshellme.bookmanager.dto.UmsRole;
import com.codeshellme.bookmanager.service.UmsRoleService;
import com.codeshellme.bookmanager.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
public class RoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    @RequestMapping("/role")
    public String role() {
        return "role";
    }

    // 查询所有菜单
    @ResponseBody
    @RequestMapping("/queryAllRoles")
    public CommonResult queryAllRoles() {
        List<Map<String, Object>> roles = umsRoleService.queryAllRoles();
        return CommonResult.success(roles);
    }


    // 查询所有菜单
    @ResponseBody
    @RequestMapping("/queryMenuTreeByRoleId")
    public CommonResult queryMenusByRoleId(@RequestParam(value = "id") Integer roleId) {
        List<Map<String, Object>> menus = umsRoleService.queryMenuTreeByRoleId(roleId);
        return CommonResult.success(menus);
    }

    // 查询所有菜单
    @ResponseBody
    @RequestMapping("/processRoleMenus")
    public CommonResult processRoleMenus(
            @RequestParam(value = "roleId") Integer roleId,
            @RequestParam(value = "menuId") Integer menuId,
            @RequestParam(value = "menuType") String menuType,
            @RequestParam(value = "checked") Boolean checked)
    {
        int ret = umsRoleService.processRoleMenus(roleId, menuId, menuType, checked);
        return CommonResult.success(ret);
    }

    // 添加角色
    @ResponseBody
    @RequestMapping("/addRole")
    public CommonResult addRole(UmsRole role) {
        int ret = umsRoleService.addRole(role);
        if (ret < 0) {
            return CommonResult.failed("添加失败");
        } else {
            return CommonResult.success("添加成功");
        }
    }

    // 删除角色
    @ResponseBody
    @RequestMapping("/deleteRoleById")
    public CommonResult deleteRoleById(@RequestParam(value = "id") Integer roleId) {
        int ret = umsRoleService.deleteRoleById(roleId);
        if (ret < 0) {
            return CommonResult.failed("删除失败");
        } else {
            return CommonResult.success("删除成功");
        }
    }

    // 更新角色
    @ResponseBody
    @RequestMapping("/updateRole")
    public CommonResult updateRole(UmsRole role) {
        System.out.println(role);
        int ret = umsRoleService.updateRole(role);
        if (ret < 0) {
            return CommonResult.failed("更新失败");
        } else {
            return CommonResult.success("更新成功");
        }
    }

    @ResponseBody
    @RequestMapping("/verifyRoleSign")
    public Boolean verifyRoleSign(String roleSign) {
        return umsRoleService.verifyRoleSign(roleSign);
    }
}
